﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using OnlineVideos.Data;
using Common.Library;
using System.ComponentModel;
using OnlineVideos.UI;
using OnlineVideos.Common;
using OnlineVideos.View_Models;
using OnlineVideos.Utilities;
using Common.Utilities;
using System.Threading.Tasks;
using OnlineVideos.Entities;

namespace OnlineVideos
{
    public partial class QuizResult : PhoneApplicationPage
    {
        QuizExitPopUp QuizPopUp;
        int displaypopup=0;
        int exitpopup = 0;
        public QuizResult()
        {
            try
            {
                InitializeComponent();
                QuizPopUp = new QuizExitPopUp();
                LoadPivotThemes(AppSettings.ShowUniqueID);
            }
            catch (Exception ex)
            {
                
               Exceptions.SaveOrSendExceptions("Exception in QuizResult Method In QuizResult.cs file.", ex);
            }
        }       
        private void LoadPivotThemes(long ShowID)
        {

            tblkVideosTitle.Text = QuizManager.GetSubjectsTitle();
        }

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                FlurryWP8SDK.Api.LogEvent("QuizResult Page", true);
                string Timeused = "";
                PageTitle.Text = AppResources.ShowQuizResultTitle;
                if (NavigationContext.QueryString.TryGetValue("Timeused", out Timeused))
                {
                    QuizManager.Displayingresult();
                    DisplayTimeUsed(Timeused);
                }
                PageHelper.RemoveEntryFromBackStack("QuizResult");
            }
            catch (Exception ex)
            {
                
                 Exceptions.SaveOrSendExceptions("Exception in PhoneApplicationPage_Loaded Method In QuizResult.cs file.", ex);
            }
        }

        private void DisplayTimeUsed(string Timeused)
        {
            //OnlineVideoDataContext context = new OnlineVideoDataContext(Constants.DatabaseConnectionString);
            try
            {
                if (Timeused != "")
                {
                    var Answerdata = Task.Run(async () => await Constants.connection.Table<QuizUserAnswers>().ToListAsync()).Result;
                    int count = Answerdata.Count();
                    if (Answerdata.Count() != Convert.ToInt32(AppSettings.ShowMaxQuestionId))
                    {
                        exitpopup = 1;
                        imgexit.Visibility = Visibility.Visible;
                        imgreview.Visibility = Visibility.Collapsed;
                    }
                    string[] displaytime = Timeused.Split(':');
                    if (displaytime[1] != "0")
                    {
                        int min = Convert.ToInt32(displaytime[1]);
                        if (min >= 1)
                            Timeused = displaytime[1] + ":" + displaytime[2];
                        else
                        {
                            if (displaytime[2] != "00")
                                Timeused = displaytime[2] + " Seconds";
                        }
                    }
                    else if (displaytime[2] != "00")
                    {
                        Timeused = displaytime[2] + "Seconds";
                    }

                    else
                        Timeused = "0";
                }
                txttime.Text = Timeused;
                if (SettingsHelper.getStringValue("QuizMarks") == "")
                    txtmarks.Text = "0";
                else
                {
                    txtmarks.Text = SettingsHelper.getStringValue("QuizMarks");
                }
                txttotalmarks.Text = AppSettings.ShowMaxQuestionId.ToString();
                QuizManager.SaveQuizResult(txtmarks.Text + " / " + txttotalmarks.Text);
                txttotaltime.Text = SettingsHelper.getStringValue("TotalTime");
                txtpshow.Text = SettingsHelper.getStringValue("QuizPercentage");
                txtgradedisplay.Text = SettingsHelper.getStringValue("QuizGrade");
                SettingsHelper.Save("QuizMarks", "");
            }
            catch (Exception ex)
            {
                
                Exceptions.SaveOrSendExceptions("Exception in DisplayTimeUsed Method In QuizResult.cs file.", ex);
            }
        }

        private void imgreview_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            AppSettings.ShowQuestionId = "1";
            NavigationService.Navigate(NavigationHelper.getQuizResultReview());
        }

        private void PhoneApplicationPage_BackKeyPress(object sender, CancelEventArgs e)
        {
            try
            {
                AppSettings.ShowQuestionId = "1";
                if (QuizPopUp.popMessage.IsOpen == false)
                {
                    QuizPopUp.Show();

                    e.Cancel = true;
                    base.OnBackKeyPress(e);
                }
                else
                {
                    QuizPopUp.Close();
                }
            }
            catch (Exception ex)
            {
                
                 Exceptions.SaveOrSendExceptions("Exception in PhoneApplicationPage_BackKeyPress Method In QuizResult.cs file.", ex);
            }
            
            
        }

        private void imgTitle_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            NavigationService.Navigate(NavigationHelper.MainPanoramaPage);
        }
        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            try
            {
                FlurryWP8SDK.Api.LogPageView();
            }
            catch (Exception ex)
            {

                Exceptions.SaveOrSendExceptions("Exception in OnNavigatedTo Method In QuizResult.cs file.", ex);
            }
        }
        protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
        {
            try
            {
                FlurryWP8SDK.Api.EndTimedEvent("QuizResult Page");
                PageHelper.RemoveEntryFromBackStack("QuizResult");
            }
            catch (Exception ex)
            {
                
                Exceptions.SaveOrSendExceptions("Exception in OnNavigatedFrom Method In QuizResult.cs file.", ex);
            }
        }

        private void imgexit_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            try
            {
                AppSettings.ShowQuestionId = "1";
                PageHelper.RemoveEntryFromBackStack("MainPage");
                NavigationService.Navigate(NavigationHelper.getQuizList(AppSettings.ShowID, tblkVideosTitle.Text));
            }
            catch (Exception ex)
            {
                
               Exceptions.SaveOrSendExceptions("Exception in imgexit_MouseLeftButtonDown Method In QuizResult.cs file.", ex);
            }
        }
    }
}